package tech.xs.common.framework.domain.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.HashMap;
import java.util.Map;

public enum HttpMethodEnum implements BaseEnum {

    GET(0),
    HEAD(1),
    POST(2),
    PUT(3),
    DELETE(4),
    CONNECT(5),
    OPTIONS(6),
    TRACE(7),
    PATCH(8);

    @EnumValue
    @JsonValue
    private final int value;

    private static final Map<Integer, HttpMethodEnum> mappings = new HashMap<>(values().length);

    static {
        for (HttpMethodEnum item : values()) {
            mappings.put(item.getValue(), item);
        }
    }

    HttpMethodEnum(int value) {
        this.value = value;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static HttpMethodEnum create(int value) {
        return mappings.get(value);
    }

    public static HttpMethodEnum create(String name) {
        for (HttpMethodEnum item : values()) {
            if (item.name().toLowerCase().equals(name.toLowerCase())) {
                return item;
            }
        }
        return null;
    }

    @Override
    public Integer getValue() {
        return value;
    }

}
